//
//  CGRemindingViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGRemindingViewController.h"
#import "CGRemindDetailViewController.h"

@interface CGRemindingViewController ()
@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)UIButton *selectAllButton;       //全部选择
@property(nonatomic,strong)UIButton *deleteButton;          //删除
@property(nonatomic,strong)UIButton *addNoticeButton;       //添加新提醒按钮
@property(nonatomic,strong)NSMutableArray *notices;

@end

@implementation CGRemindingViewController

-(NSMutableArray *)notices{
    if (!_notices) {
        _notices = [NSMutableArray array];
    }
    
    return _notices;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"信息提醒";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(enterEditing:)];
    
    [self initTableView];
    
    [self initBottomView];
    
    //使cell的线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

}

#pragma mark 添加表格
-(void)initTableView{
    
    CGRect aRect = CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT - 64 - 45);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.tableView];
    
}

-(void)initBottomView{
    
    //底部View
    CGRect bRect = CGRectMake(0, SCREEN_HEIGHT - 64, SCREEN_WIDTH, 44);
    self.bottomView = [[UIView alloc]initWithFrame:bRect];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bottomView];
    //全选Button
    CGRect cFrame = CGRectMake(0, 0, 90, 44);
    self.selectAllButton = [[UIButton alloc]initWithFrame:cFrame];
    [self.selectAllButton setImage:[UIImage imageNamed:@"mine_not_check_icon"] forState:UIControlStateNormal];
    [self.selectAllButton setImage:[UIImage imageNamed:@"mine_checked_icon"] forState:UIControlStateSelected];
    
    [self.selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
    [self.selectAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selectAllButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    //self.selectAllButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [self.bottomView addSubview:self.selectAllButton];
    //添加按钮点击事件
    [self.selectAllButton addTarget:self action:@selector(selectAllButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //删除Button
    cFrame = CGRectMake(SCREEN_WIDTH - 170, 3, 160, 38);
    self.deleteButton = [[UIButton alloc]initWithFrame:cFrame];
    [self.deleteButton setBackgroundImage:[UIImage imageNamed:@"mine_delete_bg"] forState:UIControlStateNormal];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.bottomView addSubview:self.deleteButton];
    //添加按钮点击事件
    [self.deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    //底部添加新提醒按钮
    bRect.origin.x = 20;
    bRect.origin.y = SCREEN_HEIGHT - 64 - 44;
    bRect.size.width = SCREEN_WIDTH - 40;
    bRect.size.height = 44;
    self.addNoticeButton = [[UIButton alloc]initWithFrame:bRect];
    [self.addNoticeButton setBackgroundImage:[UIImage imageNamed:@"login_btn_bg"] forState:UIControlStateNormal];
    [self.addNoticeButton setTitle:@"添加新提醒" forState:UIControlStateNormal];
    [self.view addSubview:self.addNoticeButton];
    [self.addNoticeButton addTarget:self action:@selector(addNewRemind) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark 右侧编辑按钮点击事件
-(void)enterEditing:(UIBarButtonItem *)sender{
    
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title = @"取消";
        self.addNoticeButton.hidden = YES;
        [UIView animateWithDuration:0.2 animations:^{
            self.bottomView.transform = CGAffineTransformMakeTranslation(0, -44);
        }];
        
    }else{
        sender.title = @"编辑";
        self.addNoticeButton.hidden = NO;
        [UIView animateWithDuration:0.2 animations:^{
            self.bottomView.transform = CGAffineTransformIdentity;
        }];
        
        [self cancleCheckedOfCell];
    }
    
    self.selectAllButton.selected = NO;
    [self.tableView reloadData];
}


#pragma mark 去除单元格前面的对勾
-(void)cancleCheckedOfCell{
    
    NSInteger rows =  self.notices.count;
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGNoticeCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
        cell.isCircle = NO;
    }
}

#pragma mark 选中全部单元格
-(void)selectAllcell{

    NSInteger rows =  self.notices.count;
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGNoticeCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
        cell.isCircle = YES;
    }
}

#pragma mark 全选按钮点击事件
-(void)selectAllButtonClick:(UIButton *)sender{
    
    if (self.notices.count == 0) {
        sender.selected = NO;
        return;
    }

    sender.selected = !sender.selected;
    
  
    if (sender.selected) {

        [self selectAllcell];
        
    }else{

        [self cancleCheckedOfCell];
    }
   
}


#pragma mark 删除按钮点击事件
-(void)deleteButtonClick{
    
     [self.selectAllButton setImage:[UIImage imageNamed:@"mine_not_check_icon"] forState:UIControlStateNormal];
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    NSInteger rows =  self.notices.count;
    NSMutableArray *selectedCells = [NSMutableArray array];
    if (rows == 0) {
        return;
    }
    NSLog(@"notices:%d",self.notices.count);
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGNoticeCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if (cell.isCircle) {
            //把选中的单元格集中在数组里
            NSData *data = self.notices[indexPath.row];
            [selectedCells addObject:data];
        }
    }
   //删除选中的单元格
    [self.notices removeObjectsInArray:selectedCells];
    user.notices = [NSArray arrayWithArray:self.notices];
    [user saveToSandBox];
    [self cancleCheckedOfCell];
    [self.tableView reloadData];
}


#pragma mark overide 跳转到添加新提醒页面
-(void)addNewRemind{
    
    CGAddRemindViewController *addRemindVC = [CGAddRemindViewController new];
    [self.navigationController pushViewController:addRemindVC animated:YES];
    
}



#pragma mark overide 表格的代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.notices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"myCell";
    
    CGNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CGNoticeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //给单元格属性赋值
    NSData *data = self.notices[indexPath.row];
    cell.notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        cell.circleImage.hidden = YES;
    }else{
        cell.circleImage.hidden = NO;
    }
    return cell;
    
}

//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    //把过期的时间去掉
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSData *data in user.notices) {
        CGNotice *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        //以当前时间(Now)为基准时间，返回实例保存的时间与当前时间(Now)的时间间隔
       NSTimeInterval value = [notice.date timeIntervalSinceNow];
        if (value >= 0) {
            [mArray addObject:data];
        }
    }
    self.notices = [NSMutableArray arrayWithArray:mArray];
    //[UIApplication sharedApplication].applicationIconBadgeNumber = self.notices.count;
    [self cancleCheckedOfCell];
    [self.tableView reloadData];
}

#pragma mark overide 选中时在单元格前面打勾
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        NSData *data = self.notices[indexPath.row];
        CGNotice *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    //跳转到详情页
        CGRemindDetailViewController *remindingVC = [CGRemindDetailViewController new];
        remindingVC.notice = notice;
        [self.navigationController pushViewController:remindingVC animated:YES];
        return;
    }
    
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"]) {
    
        CGNoticeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell.isCircle) {
            cell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
            cell.isCircle = YES;
        }else{
            cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
            cell.isCircle = NO;
        }
    }
}


//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
